home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////
- // AArray base class based on ABaseElement, essentially a BYTE array
- //////////////////////////////////////////////////////////////////////
- class AArray : public ABaseElement
- {
- public:
- AArray(AArray *paSource = NULL); //a_Default constructor/copy constructor!
- ~AArray();
-
- //a_Declares debug/dump related functions
- #ifdef _DEBUG_DUMP_
- void dump(void); //a_Debugging dump, when _DEBUG_DUMP_ is set
- #endif
-
- //a_Operatot access
- BYTE &operator [] (int iBYTEPos) { return _aGetBYTERef(iBYTEPos); }
- AArray &operator = (const AArray &aSource) { _bCopy(aSource); return *this; }
-
- //a_Declaration of access/assignment to elements of array
- virtual int aGetAt(int iPos) const { return _aGetBYTEValue(iPos); }
- virtual void aSetAt(int iPos, int iState, int iMode = BLIT_COPY) { aSetBYTEAt(iPos, iState, iMode); }
- virtual int aIsSet(int iPos) const { return _aGetBYTEValue(iPos); }
-
- //a_Range setting function, defaults toclearing all to 0x0, uses the virtual access members
- void aSetRange(int iState = 0x0, int iMode = BLIT_COPY, int iStart = 0x0, int iEnd = -0x1);
-
- //a_Array BYTE access/assignment
- void aSetBYTEAt(int iBYTEPos, BYTE bMask, int iMode = BLIT_COPY);
- BYTE aGetBYTEAt(int iBYTEPos) { return _aGetBYTEValue(iBYTEPos); }
-
- //a_Setting array size, assigning it to an external buffer, ...
- virtual void aSetFrom(const void *pcvSource, int iLength) { _aSetToBYTEArray((const BYTE *)pcvSource, iLength); }
- virtual int aSetSize(int iNewSize, int iRealloc = 0x0);
-
- //a_Access to the size is child dependent, this is a BYTE array so the mapping is 1:1; ABitArray is 8 times the allocated size (or thereabouts)
- virtual int aGetSize(void) const { return m_iSize; }
-
- //a_True size access
- const BYTE *aGetArray(void) const { return m_pData; }
- int aGetAllocSize(void) const { return m_iAllocSize; }
-
- //a_Blitting function; length of -1 is to blit to the end of the smaller one
- virtual void aBlit(int iMyStart, AArray &aSource, int iSourceStart, int iLength = -1, int iMode = BLIT_COPY);
-
- //ABaseElement overrides
- virtual void doOut(AStreamOutput *pasOut) const { _doArrayOutput(pasOut); }
-
- //a_AXBitmap output (image/x-xbitmap format array), mirroring the BYTEs
- void aDoXBitmap(AStreamOutput *pasOut) const { _doArrayOutput(pasOut, 0x1); }
-
- protected:
- //a_Array control methods
- void _bCopy(const AArray &aSource);
- void _aSetToBYTEArray(const BYTE *pcbSource, int iLength);
- virtual int _aMapAllocToSize(int iAllocated) { return iAllocated; } //a_This is a BYTE array
-
- //a_BYTE control
- BYTE _aGetBYTEValue(int iBYTEPos) const
- {
- assert(iBYTEPos >= 0x0 && iBYTEPos < m_iAllocSize);
- if (m_pData)
- return m_pData[iBYTEPos]; //a_Only a copy of the byte!
- else
- return 0x0;
- }
- BYTE &_aGetBYTERef(int iBYTEPos)
- {
- assert(iBYTEPos >= 0x0 && iBYTEPos < m_iAllocSize);
- assert(m_pData);
- if (m_pData) return m_pData[iBYTEPos]; //a_The BYTE itself
- else return m_bSafetyBYTE; //a_If the array doesn't exist...
- }
-
- //a_Ouput array elements in HEX form separated by comma and 8 per line
- //a_ for PBM image iPBM=1 then each bit is mirrored (reversed) as per spec
- void _doArrayOutput(AStreamOutput *pasOut, int iPBM = 0x0) const;
-
- //a_Bit counting
- int _aBitCount(BYTE bX) const
- {
- //a_# of bits in a given offset
- static char sm_bC[0x10] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 };
-
- //a_# of bits in a BYTE
- return sm_bC[bX & '\xF'] + sm_bC [(bX >> 0x4) & '\xF'];
- }
-
- //a_The array and it's properties
- BYTE *m_pData; //a_Actual data
- int m_iAllocSize; //a_Allocated size in BYTEs (true size)
- int m_iSize; //a_Specified length of array (depends on implementation!) (ie. in bit array this is 8 times the allocated size)
- BYTE m_bSafetyBYTE; //a_If the array is NULL, to prevent problems, a ref to this is returned
- };
-
- //////////////////////////////////////////////////////////////////////
- // ABit - holds one bit and modifies it, used by ABitArray
- //////////////////////////////////////////////////////////////////////
- class ABit
- {
- public:
- ABit() { m_pbData = NULL; m_iBitPos = 0x0; }
-
- friend class ABitArray; //a_For setting the protected memebers of this class
-
- void operator =(const ABit &bSource)
- {
- m_pbData = bSource.m_pbData; //a_Same pointer!
- m_iBitPos = bSource.m_iBitPos;
- }
-
- void operator =(int iState)
- {
- //a_Assignment
- assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
- if (iState) *m_pbData |= (0x1 << m_iBitPos);
- else *m_pbData &= ~(0x1 << m_iBitPos);
- }
-
- ABit &operator &=(int iState)
- {
- assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
- if (iState) *m_pbData &= (0x1 << m_iBitPos);
- else *m_pbData &= ~(0x1 << m_iBitPos);
- return *this;
- }
-
- ABit &operator |=(int iState)
- {
- assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
- if (iState) *m_pbData |= (0x1 << m_iBitPos); //a_OR only matters with 1!
- return *this;
- }
-
- ABit &operator ^=(int iState)
- {
- assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
- if (iState) *m_pbData ^= (0x1 << m_iBitPos); //a_XOR only matters with 1!
- return *this;
- }
-
- operator int() const
- {
- //a_Cast to an integer
- assert(m_iBitPos < 0x8 && m_iBitPos >= 0x0 && m_pbData);
- return ((*m_pbData & (0x1 << m_iBitPos)) ? 0x1 : 0x0);
- }
-
- protected:
- BYTE *m_pbData; //a_Pointer to the 8bit in question
- int m_iBitPos; //a_Which bit in this BYTE
- };
-
- //////////////////////////////////////////////////////////////////////
- // ABitArray - Array of 8 bits packed into a BYTE :)
- //////////////////////////////////////////////////////////////////////
- class ABitArray : public AArray
- {
- public:
- ABitArray(ABitArray *pbaSource = NULL); //a_Default and Copy constructor!
- ~ABitArray();
-
- //a_Equals operator (for tag copying)
- ABitArray &operator =(const ABitArray &baSource)
- { _bCopy(baSource); return *this; }
-
- //a_Declares debug/dump related functions
- #ifdef _DEBUG_DUMP_
- void dump(void); //a_Debugging dump, when _DEBUG_DUMP_ is set
- #endif
-
- //a_Virtual overrides from AArray
- int aSetSize(int iNewBitSize, int iRealloc = 0x0); //a_Change the size, iRealloc is valid only when shrinking and needing to reallocate
- int aGetAt(int iBitPos) const { return (_baGetAt(iBitPos) ? 0x1: 0x0); }
- int aIsSet(int iBitPos) { return _baGetAt(iBitPos); }
- void aSetAt(int iBitPos, int iState, int iMode = 0x0);
-
- //a_Set
- ABit &operator[] (int iBitPos); //a_Also a get
-
- protected:
- //a_Mapping from actual allocated size to a bit size
- virtual int _aMapAllocToSize(int iAllocated) { return iAllocated * 0x8; } //a_8 elements in each allocated unit
-
- //a_Access functions
- int _baGetAt(int iBitPos) const
- {
- //a_Gets the state of the bit (0 or 1)
- assert(iBitPos >= 0x0 && iBitPos < aGetSize());
- return int((m_pData[iBitPos / 0x8]) & BYTE(0x1UL << (0x7 - (iBitPos % 0x8))));
- }
-
- BYTE *_baGetBYTEFromBit(int iBitPos) const
- {
- //a_Gets the BYTE that the bit is in
- assert(iBitPos >= 0x0 && iBitPos < aGetSize());
- return &m_pData[iBitPos / 0x8];
- }
-
- int _baIsSetInBYTE(BYTE bX, int iBitPos) const
- {
- //a_Is set bit N in 8 bit BYTE
- assert(iBitPos < 8 && iBitPos >= 0);
- return (bX & (0x1 << iBitPos));
- }
-
- int _baIsClearInBYTE(BYTE &bX, int iBitPos) const
- {
- //a_Is clear bit N in a 8 bit BYTE
- assert(iBitPos < 8 && iBitPos >= 0);
- return (bX & ~(0x1 << iBitPos));
- }
-
- void _baSetBitInBYTE(BYTE &bX, int iBitPos, int iState)
- {
- //a_Sets the bit N in a 8 bit BYTE
- assert(iBitPos < 8 && iBitPos >= 0);
- if (iState) bX |= (0x1 << iBitPos); //a_Set
- else bX &= ~(0x1 << iBitPos); //a_Clear
- }
-
- //a_The array and it's properties
- ABit m_bIt; //a_Internal bit struct for modification purposes
- void _baSetABit(int iBitPos)
- {
- //a_This function intentionally prevents reallocation. Use SeAt instead. To prevent runaway allocations.
- assert(iBitPos < aGetSize() && iBitPos >= 0x0);
- m_bIt.m_pbData = _baGetBYTEFromBit(iBitPos);
- m_bIt.m_iBitPos = 0x7 - (iBitPos % 0x8);
- }
- };
-